@layout("/common/_container.html"){
<div class="layui-card">
	<div class="layui-form layui-card-header layuiadmin-card-header-auto">
	  <div class="layui-form-item">
	     <div class="layui-inline">
	  		<label class="layui-form-label">合同状态</label>
	   		<div class="layui-input-block">
			     <select name="contractStatus" id="contractStatus">
			       <option value="">合同状态</option>
			       <option value="0">草稿</option>
			       <option value="1">正式</option>
			     </select>
             </div>
         </div>
         <div class="layui-inline">
      		<label class="layui-form-label">合同编号</label>
			<div class="layui-input-block">
		 	 <input type="text" id="contractNo" name="contractNo" placeholder="合同编号" autocomplete="off" class="layui-input">
			</div>
       	</div>
       	<div class="layui-inline">
      		<label class="layui-form-label">订单号</label>
			<div class="layui-input-block">
		 	 <input type="text" id="orderId" name="orderId" placeholder="订单号" autocomplete="off" class="layui-input">
			</div>
       	</div>
       	<div class="layui-inline">
      		<label class="layui-form-label">申请时间</label>
			<div class="layui-input-block">
		 	 <input type="text" id="applyTime" name="applyTime" placeholder="申请时间" autocomplete="off" class="layui-input">
			</div>
       	</div>
	    <div class="layui-inline">
	      <button class="layui-btn layuiadmin-btn-useradmin" data-type="reload">
	        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
	      </button>
	    </div>
	  </div>
	</div>
  <div class="layui-card-body">
  @if(shiro.hasPermission("/contract/add")){
  <div style="padding-bottom: 10px;">
      <button class="layui-btn layuiadmin-btn-useradmin" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加</button>
  </div>
  @}
  <table class="layui-hidden" id="table" lay-filter="table"></table>
  	<script type="text/html" id="oper">
		  	@if(shiro.hasPermission("/contract/download")){
  			<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="download" title="查看合同">查看合同</a>
			@}    
			@if(shiro.hasPermission("/contract/update")){
				{{#  if(d.contractStatus != 2){ }}
					<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="update" title="作废">作废</i></a>
				{{#  } }}
			@}
			
	</script>
  </div>
</div>

<script>
layui.config({
	base: Feng.ctxPath + "/static/layuiadmin/" //静态资源所在路径
}).extend({
	index: 'lib/index' //主入口模块
}).use(['index', 'layer','table', 'form','laydate'],function(){
	
	var $ = layui.$
  	,form = layui.form
  	,table = layui.table
  	,laydate = layui.laydate;
	
	laydate.render({
	    elem: '#applyTime' //指定元素
	});
	
	var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
	table.render({
	    elem: '#table'
	    ,url: Feng.ctxPath + '/contract/list'
	    ,cellMinWidth: 100
	    ,height: 'full-168' //设置高度
	    ,cols: [[
	        {title: '合同编号', field: 'contractNo'},
	        {title: '机构', field: 'fristName'},
	        {title: '合同金额', field:'orderPrice'},
	        {title: '关联订单', field:'orderId'},
	        {title: '申请时间', field:'applyTime'},
	        {title: '状态', field:'contractStatus',templet:function (d) {
                return d.contractStatus == 0 ? "草稿合同" : (d.contractStatus == 1 ? "正式合同" : (d.contractStatus == 2 ? "作废" : ""));
            }},
	        {title: '操作',fixed: 'right', width:140, align:'center',fixed: 'right', toolbar: '#oper'}
	    ]]
	    ,page:true
	    ,loading:true
	    ,text: {
	        none: '暂无相关数据' 
	      }
	    ,done: function(res){
	    	layer.close(loadingIndex);
        }
	});
	
	//事件
	var active = {
		//加载
	   	reload: function(){
	       var contractStatus = $('#contractStatus').val();
	       var contractNo = $('#contractNo').val();
	       var orderId = $('#orderId').val();
	       var applyTime = $('#applyTime').val();
	       var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
	       //执行表格重载
	       table.reload('table', {
	    	 height: 'full-168' //设置高度
	    	 ,where: {
	    		 contractStatus: contractStatus,
	    		 contractNo: contractNo,
	    		 orderId: orderId,
	    		 applyTime: applyTime
	         }
	       ,page:true
	       ,done: function(res){
		    	layer.close(loadingIndex);
	        }
	      });
	    }
   };
  
   
   $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
     var type = $(this).data('type');
     active[type] ? active[type].call(this) : '';
   });
	

 //监听工具条
   table.on('tool(table)', function(obj){
     var data = obj.data;
     if(obj.event === 'update'){
    	 layer.confirm('确定作废此合同管理吗？', function(index){
    		 var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
    		 var ajax = new $ax(Feng.ctxPath + "/contract/update", function (data) {
                 Feng.success("操作成功!");
                 table.reload('table',{
               	  done: function(res){
          		    	layer.close(loadingIndex);
          	          }
                  }); //数据刷新
                 layer.close(index); //关闭弹层
             }, function (data) {
                 Feng.error("操作失败!" + data.responseJSON.message + "!");
             });
             ajax.set("contractId", data.contractId);
             ajax.start();
         });
     }else if(obj.event === 'download'){
    	 // alert(data.contractId);
		 layer.alert('合同自动下载中，完成后请自行查看~', {
			 skin: 'layui-layer-molv' //样式类名
			 ,closeBtn: 0
		 });
    	 location.href = Feng.ctxPath + '/contract/download?contractId='+data.contractId;
     }
   });
});



   
</script>
@}
